//
//  ChangeRemindViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  修改续卡提醒条件

#import "ChangeRemindViewController.h"

@interface ChangeRemindViewController ()

@property(nonatomic,weak)UIScrollView *scrollView;

@property(nonatomic,weak)CommentSwitchView *valueSwitchView;

@property(nonatomic,weak)CommonInputView *valueInputView;

@property(nonatomic,weak)CommentSwitchView *timeSwitchView;

@property(nonatomic,weak)CommonInputView *timeInputView;

@property(nonatomic,weak)CommentSwitchView *daySwitchView;

@property(nonatomic,weak)CommonInputView *dayInputView;

@property(nonatomic,weak)TTTAttributedLabel *noticeLabel;

@end

@implementation ChangeRemindViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title = @"自动提醒";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
}

-(void)createSubView{

    self.view.backgroundColor = BackGroundColor;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    [scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
    }];
    
    UILabel *autoNoticeLabel = [[UILabel alloc] init];
    autoNoticeLabel.text =  @"自动提醒设置";
    autoNoticeLabel.font = MidFont;
    autoNoticeLabel.textColor = CSColorFromRGB(0x999999);
    [scrollView addSubview:autoNoticeLabel];
    [autoNoticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(scrollView).offset(10);
        make.top.mas_equalTo(scrollView).offset(20);
    }];
    
    CommentSwitchView *valueSwitchView = [[CommentSwitchView alloc] init];
    valueSwitchView.titleLabel.text = @"储值卡余额不足时提醒会员";
    [valueSwitchView.titleSwitch setOn:YES];
    [valueSwitchView.titleSwitch addTarget:self action:@selector(valueSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
    [scrollView addSubview:valueSwitchView];
    valueSwitchView.titleLabel.width = 150;
    self.valueSwitchView = valueSwitchView;
    [valueSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {       
        make.top.mas_equalTo(autoNoticeLabel.mas_bottom).offset(20);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    CommonInputView *valueInputView = [[CommonInputView alloc] init];
    valueInputView.titleLabel.text = @"充值卡余额少于（元）";
    [scrollView addSubview:valueInputView];
    self.valueInputView = valueInputView;
    [valueInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(valueSwitchView.mas_bottom);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    CommentSwitchView *timeSwitchView = [[CommentSwitchView alloc] init];
    timeSwitchView.titleLabel.text = @"次卡余额不足时提醒会员";
    [timeSwitchView.titleSwitch setOn:YES];
    [timeSwitchView.titleSwitch addTarget:self action:@selector(timeSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
    [scrollView addSubview:timeSwitchView];
    self.timeSwitchView = timeSwitchView;
    [timeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(valueInputView.mas_bottom).offset(10);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    CommonInputView *timeInputView = [[CommonInputView alloc] init];
    timeInputView.titleLabel.text = @"次卡余额少于（次）";
    [scrollView addSubview:timeInputView];
    self.timeInputView = timeInputView;
    [timeInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(timeSwitchView.mas_bottom);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    CommentSwitchView *daySwitchView = [[CommentSwitchView alloc] init];
    daySwitchView.titleLabel.text = @"会员卡到期时提醒会员";
    [daySwitchView.titleSwitch setOn:YES];
    [daySwitchView.titleSwitch addTarget:self action:@selector(daySwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
    [scrollView addSubview:daySwitchView];
    self.daySwitchView = daySwitchView;
    [daySwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(timeInputView.mas_bottom).offset(10);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    CommonInputView *dayInputView = [[CommonInputView alloc] init];
    dayInputView.titleLabel.text = @"会员卡有效期少于（天）";
    [scrollView addSubview:dayInputView];
    self.dayInputView = dayInputView;
    [dayInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(daySwitchView.mas_bottom);
        make.left.width.mas_equalTo(scrollView);
    }];
    
    TTTAttributedLabel *noticeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    noticeLabel.font = MidFont;
    noticeLabel.numberOfLines = 0;
    noticeLabel.lineSpacing = 15;
    noticeLabel.textColor = CSColorFromRGB(0x999999);
    noticeLabel.text = @"设置说明\n1、系统自动发送提醒短信给会员卡余额不足的会员\n2、APP发送系统通知提醒该会员的销售";
    [scrollView addSubview:noticeLabel];
    self.noticeLabel = noticeLabel;
    [noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(dayInputView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(scrollView);
    }];
    
    [self setScrollViewContentSize];
}


-(void)setScrollViewContentSize{
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
    self.scrollView.contentSize = CGSizeMake(self.view.width, self.noticeLabel.bottom + 10 + adaptNavagationBar);
}

-(void)valueSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        CommonInputView *valueInputView = [[CommonInputView alloc] init];
        valueInputView.titleLabel.text = @"充值卡余额少于（元）";
        [self.scrollView addSubview:valueInputView];
        self.valueInputView = valueInputView;
        [valueInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.valueSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self.scrollView);
        }];
        
        [self.timeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(valueInputView.mas_bottom).offset(10);
            make.left.width.mas_equalTo(self.scrollView);
        }];
    }
    else{
        
        [self.valueInputView removeFromSuperview];
        
        [self.timeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.valueSwitchView.mas_bottom).offset(10);
            make.left.width.mas_equalTo(self.scrollView);
        }];
    }
    
    [self setScrollViewContentSize];
}

-(void)timeSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        CommonInputView *timeInputView = [[CommonInputView alloc] init];
        timeInputView.titleLabel.text = @"次卡余额少于（次）";
        [self.scrollView addSubview:timeInputView];
        self.timeInputView = timeInputView;
        [timeInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.timeSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self.scrollView);
        }];
        
        [self.daySwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(timeInputView.mas_bottom).offset(10);
            make.left.width.mas_equalTo(self.scrollView);
        }];
    }
    else{
        
        [self.timeInputView removeFromSuperview];
        
        [self.daySwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.timeSwitchView.mas_bottom).offset(10);
            make.left.width.mas_equalTo(self.scrollView);
        }];
    }
    
    [self setScrollViewContentSize];
}

-(void)daySwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        CommonInputView *dayInputView = [[CommonInputView alloc] init];
        dayInputView.titleLabel.text = @"会员卡有效期少于（天）";
        [self.scrollView addSubview:dayInputView];
        self.dayInputView = dayInputView;
        [dayInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.daySwitchView.mas_bottom);
            make.left.width.mas_equalTo(self.scrollView);
        }];
        
        [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(dayInputView.mas_bottom).offset(20);
            make.centerX.mas_equalTo(self.scrollView);
        }];
    }
    else{
        
        [self.dayInputView removeFromSuperview];
    
        [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.daySwitchView.mas_bottom).offset(20);
            
            make.centerX.mas_equalTo(self.scrollView);
        }];
    }
    
    [self setScrollViewContentSize];
}


#pragma mark 保存
-(void)rightBtnClick{
    
    //Post_MemberCardNotifySetOperate
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
//    
//    if (self.valueInputView) {
//        
//        [params setObject:self.valueInputView.titleTF.text forKey:@"Balance"];
//    }
//    
//    if (self.timeInputView) {
//        
//        [params setObject:self.timeInputView.titleTF.text forKey:@"Times"];
//    }
//    
//    if (self.valueInputView) {
//        
//        [params setObject:self.timeInputView.titleTF.text forKey:@"Times"];
//    }
    
    [HTTPTool POSTRaw:Post_MemberCardNotifySetOperate parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  [MBProgressHUD showText:@"修改成功"];
                  
                  [NotificationCenter postNotificationName:@"ReloadRenwalCard" object:nil];
                  
                  [self.navigationController popViewControllerAnimated:YES];
              }
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
}
@end
